<!--
	Carnegie Mellon University
	School of Computer Science
	MSIT eBusiness Technology

	Team 8 - Infinity (2011 Spring)
	infinity-team8@googlegroups.com

	Professor: Jeffrey L. Eppinger
	Program Faculty: Sujata Telang
-->

<jsp:include page="employee-template-top.jsp" />
<jsp:include page="employee-topnav.jsp" />

<%@ page import="databean.CustomerBean" %>
<%@ page import="databean.PositionBean" %>
<%@ page import="model.CustomerDAO" %>
<%@ page import="model.PositionDAO" %>
<%@ page import="model.GlobalDAO" %>
<%@ page import="model.FundPriceHistoryDAO" %>
<%@ page import="model.FundDAO" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page import="java.text.NumberFormat" %>

<div id="header">
	<h1>View Customer Account</h1>
</div>

<jsp:include page="employee-sidebar.jsp" />

<div id="content">

	<div id="form-wrapper">


<%
	CustomerBean customer = (CustomerBean) request.getAttribute("customer");
	PositionBean[] positions = (PositionBean[]) request.getAttribute("positions");
%>
	<h2><%=customer.getFirstName()%> <%=customer.getLastName()%></h2>
	
	
	<%=customer.getAddressLine1()%><br/>
	<%
		if (customer.getAddressLine2()!=null) {
	%>
		<%=customer.getAddressLine2()%><br/>
	<%
		}
	%>	
	<%=customer.getCity()%>, <%=customer.getState()%> <%=customer.getZip()%><br/><br/>
	
	
	<%
		CustomerDAO customerDAO = new CustomerDAO();
		PositionDAO positionDAO = new PositionDAO();
		FundDAO fundDAO = new FundDAO();
		FundPriceHistoryDAO fundPriceHistoryDAO = new FundPriceHistoryDAO();
		GlobalDAO globalDAO = new GlobalDAO();
	%>
	
		<div class="box">

			<label>Cash Balance  </label> $<%=customerDAO.getCashFormatted(customer.getUserName())%><br/>
			<label>Available Cash  </label> $<%=customerDAO.getAvailableCashFormatted(customer.getUserName())%>
		</div>
	
	<%
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String lastTradingDay;
		
		if (customerDAO.getLastTradingDay(customer.getCustomerID()) == null) {
			lastTradingDay = "Unavailable";
		} else {
			Date d = customerDAO.getLastTradingDay(customer.getCustomerID());
			lastTradingDay = sdf.format(d);
		}		
	%>
	
		<label>Date of your last trading day  </label> <%=lastTradingDay%><br/><br/>
	

		
	
	<%
	  if (positions != null) {
		for (PositionBean position : positions) {
	%>
		<label>Number of shares of each fund owned and the value of that position</label><br/>
		
		<table id="minimal" summary="current share of each fund for this customer">
			<thead>
				<tr>
					<th scope="col"> Fund Symbol </th>
					<th scope="col"> Shares </th>
					<th scope="col"> Available shares </th>
					<th scope="col"> Value </th>
				</tr>
			</thead>
				
			<tbody>
				<tr>
					<td><%=fundDAO.lookup(position.getFundID()).getSymbol()%></td>
					<td><%=positionDAO.getSharesFormatted(position.getCustomerID(), position.getFundID())%></td>
					<td><%=positionDAO.getAvailableSharesFormatted(position.getCustomerID(), position.getFundID())%></td>
					
					<%
			String value;
			if (globalDAO.lookup("lastTradingDay")==null) {
				value = "Unavailable";
			} else {
				double temp = Double.parseDouble(positionDAO.getSharesFormatted(position.getCustomerID(), position.getFundID())) *
				Double.parseDouble(fundPriceHistoryDAO.getPriceFormatted(position.getFundID(), globalDAO.lookup("lastTradingDay").getDate()));
				
				NumberFormat formatter = new DecimalFormat("##0.00");
				value = "$" + formatter.format(temp);
			}
		%>
		
					<td><%=value%></td>
				</tr>
			</tbody>
		</table>


	<%
		}
	  } else  {
	%>
	    <br/>No positions for this customer...
	<%
		}	 
	%>

	</div>
</div>

<jsp:include page="footer.jsp" />
